/*
 * Copyright 万达信息股份有限公司.
 */
package com.rohoo.framework.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.rohoo.framework.dao.DataPackage;
/**
 * 分页标签
 * @author futianqi
 *
 */
public class PageNavTag extends TagSupport {

	private String dataPackage;
	private String css;

	public void setDataPackage(String dataPackage) {
		this.dataPackage = dataPackage;
	}
	

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}


	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.Tag#doEndTag()
	 */
	public int doEndTag() throws JspException {
		DataPackage bean = (DataPackage) pageContext.getRequest().getAttribute(
				dataPackage);
		int currentPage=0;
		int pageCount=0;
		int rowCount = 0;
		int pageLines = 0;
		if(bean!=null)
		{
			 currentPage = bean.getCurrPage();
			 pageCount = bean.getPageCount();
			 rowCount = bean.getRowCount();
			 pageLines = bean.getPageLines();
		}else
		{
			currentPage = Integer.parseInt((String)pageContext.getRequest().getAttribute("currPage"));
			pageCount = Integer.parseInt((String)pageContext.getRequest().getAttribute("pageCount"));
			rowCount = Integer.parseInt((String)pageContext.getRequest().getAttribute("rowCount"));
			pageLines = Integer.parseInt((String)pageContext.getRequest().getAttribute("perPageNum"));
		}
		
		
		
		StringBuffer html = new StringBuffer();
		
		html.append("�?font color='red'>" + rowCount + "</font>条记录，每页<font color='red'>"+ pageLines +"</font>条，");
		html = html.append("当前�?font color='red'>").append(currentPage).append("</font>�?<font color='red'>").append(
				pageCount).append("</font>�?nbsp;&nbsp;");
		
		if (currentPage > 1) {
			if(css!=null&&css.trim().length()>0)
			{html = html
					.append("<a class="+css+" href='javascript:showFirstPage()'>首页</a>&nbsp;");

			html = html
					.append("<a class="+css+" href='javascript:showPreviousPage()'>上页</a>&nbsp;");
			}
			else
				{
				 html = html
				.append("<a  href='javascript:showFirstPage()'>首页</a>&nbsp;");}
			    
			}
		
		if (currentPage < pageCount) {
			if(css!=null&&css.trim().length()>0){
				html = html
						.append("<a class="+css+" href='javascript:showNextPage()'>下页</a>&nbsp;");
				html = html
						.append("<a class="+css+" href='javascript:showLastPage()'>末页</a>&nbsp;");
			}else{
				html = html
					.append("<a href='javascript:showNextPage()'>下页</a>&nbsp;");
			    html = html
					.append("<a href='javascript:showLastPage()'>末页</a>&nbsp;");
			}
		}

		if (pageCount >= 1) {
			html = html.append("&nbsp;转到");
			html = html.append("<select name='_selectpage' onChange='jumpPage()' class='list-srchselect'>");
			for (int i = 1; i <= pageCount; i++) {
				if (currentPage == i) {
					html = html.append("<option value='" + i + "' selected>"
							+ i + "</option>");
				} else {
					html = html.append("<option value='" + i + "'>" + i
							+ "</option>");
				}
			}
			html = html.append("</select>");
			html = html.append("�?nbsp;");
		}
		else {
			html = html.append("&nbsp;转到");
			html = html.append("<select name='_selectpage' class='list-srchselect'>");
			html = html.append("<option value=''>" + " " + "</option>");
			html = html.append("</select>");
			html = html.append("�?nbsp;");
		}
		
		
		try {
			pageContext.getOut().print(html.toString());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return super.doEndTag();
	}
}
